
import fs from "fs";
import path from "path";

//百度翻译的语种编码
export const baiduTranslatekeyMap = {
  ar: "ara", //阿拉伯语
  bn: "ben",
  de: "de",
  el: "el",
  en: "en",
  es: "spa",
  fa: "per",
  fil: "fil",
  fr: "fra",
  hi: "hi",
  id: "id",
  it: "it",
  ja: "jp",
  ko: "kor",
  lo: "lao",
  ms: "may",
  my: "bur",
  pt: "pt",
  ru: "ru",
  th: "th",
  tr: "tr",
  ur: "urd",
  vi: "vie",
  zh: "zh",
};
export function getPathFileList(dirPath,source) {
  let fileList = [];
  const files = fs.readdirSync(dirPath);
  files.forEach((file) => {
    const filePath = path.join(dirPath, file); // 使用 path.join 替代字符串拼接
    const stats = fs.statSync(filePath);
    if (stats.isDirectory()) {
      fileList = fileList.concat(getPathFileList(filePath)); // 修复函数名错误
    } else {
      let fileName = file.split(".")[0];
      let suffix = file.split(".")[1]
      if (fileName != source||fileName!='index') {
        fileList.push({
          path: filePath,
          name: fileName,
          suffix
        });
      }
    }
  });
  return fileList;
}


export function getFileContent(filePath) {
  return new Promise((resolve, reject) => {
    fs.readFile(filePath, "utf8", (err, data) => {
      if (err) {
        reject(err);
      } else {
        resolve(data);
      }
    });
  });
}

// 使用prettier格式化文件
export async function formatWithPrettier(filePath) {
  // 如果prettier不可用，则直接返回
  // 尝试引入prettier
  let prettier;
  try {
    prettier = await import("prettier");
  } catch (error) {
    console.log("Prettier未安装，将跳过格式化步骤");
  }

  try {
    const text = fs.readFileSync(filePath, "utf8");
    const prettierConfig = await prettier.resolveConfig(filePath);
    const formattedText = await prettier.format(text, {
      ...prettierConfig,
      filepath: filePath,
    });
    fs.writeFileSync(filePath, formattedText);
    console.log(`文件 ${filePath} 已使用 Prettier 格式化`);
  } catch (error) {
    console.error(`Prettier 格式化文件 ${filePath} 失败:`, error.message);
  }
}